home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / PRUS101.ZIP / FCHARS.PAS < prev    next >
Pascal/Delphi Source File  |  1994-12-19  |  14KB  |  413 lines

  1. UNIT FCHARS; { FIDO unit to add Borland Pascal 7.0's Strings functions to
  2.                prior versions }
  3.  (***************************************************************************
  4.  
  5.         RELEASE 1.00 - as first contained in the file PRUS101.LZH
  6.               by Sieghard Schicktanz, 2:2480/642.25, GERMANY
  7.  
  8.                --------------------------------------------
  9.                 organized for Fido's PASCAL related echoes
  10.                --------------------------------------------
  11.  
  12.    08/02/1994 to --/--/---- by Sieghard Schicktanz, 2:2480/642.25, GERMANY
  13.  
  14.  
  15.            As far as third party copyrights are not violated this
  16.            source code is hereby placed to the public domain. Use
  17.            it whatever way you want, but use AT YOUR OWN RISK.
  18.  
  19.            In case you should modify the source rather send your
  20.            modifications to the unit's current organizer (see above for
  21.            NM address) than to spread it on your own. This will help to
  22.            keep the unit updated and grant a certain standard to all
  23.            other users as well.
  24.  
  25.            The unit is currently still under work. So it might greatly
  26.            benefit of your participation.
  27.  
  28.            Those who contributed to the following piece of source,
  29.            listed in alphabethical order:
  30.         ================================================================
  31.            Andrew Eigus, Sieghard Schicktanz, ...
  32.         ================================================================
  33.            YOUR NAME WILL APPEAR HERE IF YOU CONTRIBUTE USEFUL SOURCE.
  34.  
  35.            Credits in your own programs are as welcome as unnecessary.
  36.  
  37.  ***************************************************************************)
  38.  
  39. { $I FDEFINE.DEF}
  40.  
  41. INTERFACE
  42.  
  43. TYPE
  44.   chArray = ARRAY [0..MaxInt] OF char;  { just a dummy definition }
  45.   PChar = ^chArray;
  46.  
  47. FUNCTION null_string: PChar;
  48. { returns pointer to a string which is always empty }
  49.  
  50. FUNCTION Str2PChar (str: string): PChar;
  51. { alternate function for StrPCopy, both functions
  52.   work the same way, except that Str2PChar does not
  53.   expect the destination to be given as an argument. }
  54.  
  55. FUNCTION StrCat (dest, source: PChar): PChar;
  56. { same as the Borland Pascal function;
  57.   appends source to dest and returns a pointer
  58.   which points to the modified dest's head.
  59.   dest must be large enough to hold dest+source,
  60.   otherwise you will face a grinding problem. }
  61.  
  62. FUNCTION StrComp (dest, source: PChar): integer;
  63. { same as the Borland Pascal function;
  64.   compares dest and source returning 0, if both
  65.   are equal, -1 if dest > source and +1 if dest < source. }
  66.  
  67. FUNCTION StrCopy (dest, source: PChar): PChar;
  68. { same as the Borland Pascal function;
  69.   copies source to dest and returns a
  70.   pointer which points to the new copy's head.
  71.   dest must be large enough to contain source,
  72.   otherwise you will face a grinding problem. }
  73.  
  74. PROCEDURE StrDispose (source: PChar);
  75. { same as the Borland Pascal function;
  76.   destroys source previously created by StrNew,
  77.   doesn't do anything when passed a NIL pointer. }
  78.  
  79. FUNCTION StrECopy (dest, source: PChar): PChar;
  80. { same as the Borland Pascal function;
  81.   copies source to dest and returns a
  82.   pointer which points to the new copy's tail.
  83.   dest must be large enough to contain source,
  84.   otherwise you will face a grinding problem. }
  85.  
  86. FUNCTION StrEnd (source: PChar): PChar;
  87. { same as the Borland Pascal function;
  88.   returns a pointer to the end of the source string }
  89.  
  90. FUNCTION StrLCat (dest, source: PChar; maxlen: byte): PChar;
  91. { same as the Borland Pascal function;
  92.   appends source to dest for maxlen characters
  93.   and returns a pointer which points to the new
  94.   dest's head;
  95.   dest must be large enough to hold dest plus maxlen
  96.   characters of source, otherwise you will face
  97.   a grinding problem. }
  98.  
  99. FUNCTION StrLComp (dest, source: PChar; maxlen: byte): integer;
  100. { same as the Borland Pascal function;
  101.   compares first maxlen characters of dest and source;
  102.   returns 0 if equal, -1 if dest > source and +1 if dest <
  103.   source. }
  104.  
  105. FUNCTION StrLCopy (dest, source: PChar; maxlen: byte): PChar;
  106. { same as the Borland Pascal function;
  107.   copies source to dest for maxlen characters,
  108.   returns a pointer which points to the new
  109.   copy's head;
  110.   dest must be large enough to hold maxlen characters,
  111.   otherwise you will face a grinding problem. }
  112.  
  113. FUNCTION StrLen (source: PChar): word;
  114. { same as the Borland Pascal function;
  115.   returns the length of the source }
  116.  
  117. FUNCTION StrLower (source: PChar): PChar;
  118. { same as the Borland Pascal function;
  119.   converts all characters in source to lower case }
  120.  
  121. FUNCTION StrMove (dest, source: PChar; maxlen: byte): PChar;
  122. { same as the Borland Pascal function;
  123.   copies source to dest for maxlen characters,
  124.   returns a pointer which points to the new
  125.   copy's head; dest may overlap source;
  126.   dest must be large enough to hold maxlen characters,
  127.   otherwise you will face a grinding problem. }
  128.  
  129. FUNCTION StrNew (source: PChar): PChar;
  130. { same as the Borland Pascal function;
  131.   duplicates source by generating a new copy on the
  132.   heap and returns a pointer which points to the new
  133.   copy. }
  134.  
  135. FUNCTION StrNSkip (source: PChar; positions: integer): PChar;
  136. { internal supporting function, which probably may also
  137.   be useful within your own sources; will advance the initial
  138.   pointer in source by positions characters }
  139.  
  140. FUNCTION StrPas (source: PChar): string;
  141. { converts a C-string into Turbo Pascal format;
  142.   source string may consist of up to 255 characters,
  143.   otherwise it will be truncated }
  144.  
  145. FUNCTION StrPCopy (dest: PChar; source: string): PChar;
  146. { converts a Turbo Pascal string into C format;
  147.   StrPCopy generates a new copy on the heap and will
  148.   return a pointer to that new copy }
  149.  
  150. FUNCTION StrPos (teststr, substr: PChar): PChar;
  151. { same as the Borland Pascal function;
  152.   searches teststr for substr, if substr can be found
  153.   returns a pointer to substr's position, else
  154.   returns NIL. }
  155.  
  156. FUNCTION StrRScan (source: PChar; ch: char): PChar;
  157. { same as the Borland Pascal function;
  158.   searches source for the _last_ occurrence of ch; if
  159.   ch can be found it returns a pointer to ch,
  160.   otherwise it returns NIL }
  161.  
  162. FUNCTION StrScan (source: PChar; ch: char): PChar;
  163. { same as the Borland Pascal function;
  164.   searches source for the first occurrence of ch; if
  165.   ch can be found it returns a pointer to ch,
  166.   otherwise it returns NIL }
  167.  
  168. FUNCTION StrSkip (source: PChar): PChar;
  169. { internal supporting function, which probably may also
  170.   be useful within your own sources; will set the
  171.   initial pointer to the source's end (after the
  172.   terminating NUL) }
  173.  
  174. FUNCTION StrUpper (source: PChar): PChar;
  175. { same as the Borland Pascal function;
  176.   converts all characters in source to upper case }
  177.  
  178.  
  179. IMPLEMENTATION
  180.  
  181.  
  182. CONST
  183.   nullstring: char {chArray} = (#0);
  184.  
  185.  
  186. FUNCTION LowCase (InChar: char): char;
  187.  BEGIN (* LowCase *)
  188.    IF ('A' <= InChar) AND (InChar <= 'Z')
  189.      THEN LowCase:= chr (ord (InChar)- ord ('A')+ ord ('a'))
  190.      ELSE
  191.        CASE InChar OF
  192.          'Ä': LowCase:= 'ä';
  193.          'Ö': LowCase:= 'ö';
  194.          'Ü': LowCase:= 'ü';
  195.          ELSE LowCase:= InChar;
  196.        END (* CASE InChar *);
  197.  END (* LowCase *);
  198.  
  199.  
  200. {$L Fchars }
  201.  
  202. { -------------------------------------------------------------------------- }
  203.  
  204. FUNCTION null_string: PChar;
  205.  BEGIN (* null_string *)
  206.    null_string:= @nullstring;
  207.  END  (* null_string *);
  208.  
  209.  
  210. FUNCTION Str2PChar (str: string): PChar; EXTERNAL;
  211.  
  212. (* Too dangerous to use...
  213.  
  214. Function Str2PChar(str: string): PChar; assembler;
  215. { Original author: Andrew Eigus }
  216. Asm
  217.   LES DI,Str
  218.   MOV AL,BYTE PTR [ES:DI]
  219.   CMP AL,0
  220.   JE  @@1
  221.   PUSH DI
  222.   XOR AH,AH
  223.   CLD
  224.   INC AL
  225.   STOSB
  226.   ADD DI,AX
  227.   DEC DI
  228.   XOR AL,AL  { MOV AL,0 }
  229.   STOSB
  230.   POP DI
  231. @@1:
  232.   INC DI
  233.   MOV DX,ES
  234.   MOV AX,DI
  235. End; { Pas2PChar }
  236.  *)
  237. { -------------------------------------------------------------------------- }
  238.  
  239. FUNCTION StrCat (dest, source: PChar): PChar; EXTERNAL;
  240. { Original author: Sieghard Schicktanz }
  241.  
  242. { -------------------------------------------------------------------------- }
  243.  
  244. FUNCTION StrComp (dest, source: PChar): integer; EXTERNAL;
  245. { Original author: Sieghard Schicktanz }
  246.  
  247. { -------------------------------------------------------------------------- }
  248.  
  249. FUNCTION StrCopy (dest, source: PChar): PChar; EXTERNAL;
  250. { Original author: Sieghard Schicktanz }
  251.  
  252. { -------------------------------------------------------------------------- }
  253.  
  254. PROCEDURE StrDispose (source: PChar);
  255. { Original author: Sieghard Schicktanz }
  256.  VAR
  257.    size: word;
  258.  BEGIN (* StrDispose *)
  259.    IF source <> NIL THEN BEGIN
  260.      size:= StrLen (source);
  261.      FreeMem (source, succ (size));
  262.    END (* IF source <> NIL *)
  263.  END (* StrDispose *);
  264.  
  265. { -------------------------------------------------------------------------- }
  266.  
  267. FUNCTION StrECopy (dest, source: PChar): PChar; EXTERNAL;
  268. { Original author: Sieghard Schicktanz }
  269.  
  270. { -------------------------------------------------------------------------- }
  271.  
  272. FUNCTION StrEnd (source: PChar): PChar; EXTERNAL;
  273. { Original author: Sieghard Schicktanz }
  274.  
  275. { -------------------------------------------------------------------------- }
  276.  
  277. FUNCTION StrLCat (dest, source: PChar; maxlen: byte): PChar; EXTERNAL;
  278. { Original author: Sieghard Schicktanz }
  279.  
  280. { -------------------------------------------------------------------------- }
  281.  
  282. FUNCTION StrLComp (dest, source: PChar; maxlen: byte): integer; EXTERNAL;
  283. { Original author: Sieghard Schicktanz }
  284.  
  285. { -------------------------------------------------------------------------- }
  286.  
  287. FUNCTION StrLCopy (dest, source: PChar; maxlen: byte): PChar; EXTERNAL;
  288. { Original author: Sieghard Schicktanz }
  289.  
  290. { -------------------------------------------------------------------------- }
  291.  
  292. FUNCTION StrLen (source: PChar): word; EXTERNAL;
  293. { Original author: Sieghard Schicktanz }
  294.  
  295. { -------------------------------------------------------------------------- }
  296.  
  297. FUNCTION StrLower (source: PChar): PChar;
  298. { Original author: Sieghard Schicktanz }
  299.  VAR
  300.    i: word;
  301.  BEGIN (* StrLower *)
  302.    StrLower:= source; i:= 0;
  303.    {$V-}
  304.    WHILE source^ [i] <> #0 DO BEGIN
  305.      source^ [i]:= LowCase (source^ [i]); Inc (i);
  306.    END (* WHILE source^ [i] <> #0 *);
  307.    {$V+}
  308.  END (* StrLower *);
  309.  
  310. { -------------------------------------------------------------------------- }
  311.  
  312. FUNCTION StrMove (dest, source: PChar; maxlen: byte): PChar;
  313. { Original author: Sieghard Schicktanz }
  314.  VAR
  315.    size: word;
  316.  BEGIN (* StrMove *)
  317.    size:= StrLen (source);
  318.    IF (source <> NIL) AND (size <> 0)
  319.      THEN Move (source, dest, succ (size));
  320.    StrMove:= dest;
  321.  END (* StrMove *);
  322.  
  323. { -------------------------------------------------------------------------- }
  324.  
  325. FUNCTION StrNew (source: PChar): PChar;
  326. { Original author: Sieghard Schicktanz }
  327.  VAR
  328.    size: word;
  329.    dest: PChar;
  330.  BEGIN (* StrNew *)
  331.    size:= StrLen (source);
  332.    IF (source <> NIL) AND (size <> 0) THEN BEGIN
  333.      GetMem (dest, succ (size));
  334.      StrNew:= StrCopy (dest, source);
  335.    END (* IF (source <> NIL) AND (size <> 0) *)
  336.    ELSE StrNew:= NIL;
  337.  END (* StrNew *);
  338.  
  339. { -------------------------------------------------------------------------- }
  340.  
  341. FUNCTION StrNSkip (source: PChar; positions: integer): PChar;
  342. { Original author: Sieghard Schicktanz }
  343.  BEGIN (* StrNSkip *)
  344.    StrNSkip:= Ptr (Seg (source^), Ofs (source^)+ positions);
  345.  END (* StrNSkip *);
  346.  
  347. { -------------------------------------------------------------------------- }
  348.  
  349. FUNCTION StrPas (source: PChar): string; EXTERNAL;
  350. { Original author: Sieghard Schicktanz }
  351.  
  352. { -------------------------------------------------------------------------- }
  353.  
  354. FUNCTION StrPCopy (dest: PChar; source: string): PChar; { EXTERNAL; }
  355. { Original author: Sieghard Schicktanz }
  356.  BEGIN (* StrPCopy *)
  357.    GetMem (dest, Length (source)+ 1);
  358.    Move (source [1], dest^, Length (source));
  359.    {$V-} dest^ [Length (source)]:= #0; {$V+}
  360.    StrPCopy:= dest;
  361.  END (* StrPCopy *);
  362.  
  363. { -------------------------------------------------------------------------- }
  364.  
  365. FUNCTION StrPos (teststr, substr: PChar): PChar;
  366. { Original author: Sieghard Schicktanz }
  367.  VAR
  368.    temp: PChar;
  369.  BEGIN (* StrPos *)
  370.    temp:= StrNSkip (teststr, -1);
  371.    REPEAT
  372.      temp:= StrScan (StrNSkip (temp, 1), substr^ [0]);
  373.    UNTIL (temp = NIL) OR (StrComp (temp, substr) <= 0);
  374.    StrPos:= temp;
  375.  END (* StrPos *);
  376.  
  377. { -------------------------------------------------------------------------- }
  378.  
  379. FUNCTION StrRScan (source: PChar; ch: char): PChar; EXTERNAL;
  380. { Original author: Sieghard Schicktanz }
  381.  
  382. { -------------------------------------------------------------------------- }
  383.  
  384. FUNCTION StrScan (source: PChar; ch: char): PChar; EXTERNAL;
  385. { Original author: Sieghard Schicktanz }
  386.  
  387. { -------------------------------------------------------------------------- }
  388.  
  389. FUNCTION StrSkip (source: PChar): PChar; EXTERNAL;
  390. { Original author: Sieghard Schicktanz }
  391.  
  392. { -------------------------------------------------------------------------- }
  393.  
  394. FUNCTION StrUpper (source: PChar): PChar;
  395. { Original author: Sieghard Schicktanz }
  396.  VAR
  397.    i: word;
  398.  BEGIN (* StrUpper *)
  399.    StrUpper:= source; i:= 0;
  400.    {$V-}
  401.    WHILE source^ [i] <> #0 DO BEGIN
  402.      source^ [i]:= UpCase (source^ [i]); Inc (i);
  403.    END (* WHILE source^ [i] <> #0 *);
  404.    {$V+}
  405.  END (* StrUpper *);
  406.  
  407. { -------------------------------------------------------------------------- }
  408.  
  409. {$IFnDEF Overlays}
  410. (* BEGIN (* Fstrings *)
  411. {$ENDIF}
  412. END (* Fstrings *).
  413.